package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.SearchClassifiesBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinda Yang on 2018/4/1.
 * Description : 搜索界面下拉菜单列表的适配器
 */

public class SearchClassificationDropMenuAdapter extends BaseRecyclerViewAdapter<SearchClassifiesBean> {

    /**
     * context 获取activity的context关系
     */
    private Context mContext;

    public SearchClassificationDropMenuAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = getConvertView(mContext, R.layout.item_search_classify, null);
        return new ClassifiesViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SearchClassifiesBean bean = mList.get(position);
        if (holder instanceof ClassifiesViewHolder) {
            ((ClassifiesViewHolder) holder).setData(bean);
            ((ClassifiesViewHolder) holder).setListener(bean);
        }
    }

    class ClassifiesViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_classify_name)
        TextView mTvClassifyName;

        ClassifiesViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        /**
         * 将数据填充到视图上
         *
         * @param bean 类别数据
         */
        public void setData(SearchClassifiesBean bean) {

            // 选中状态字体颜色为#67A0FF 未选中字体颜色为#000000
            int textColor;
            if (bean.isSelected()) {
                textColor = ContextCompat.getColor(mContext, R.color.blue_67a0ff);
            } else {
                textColor = ContextCompat.getColor(mContext, R.color.black);
            }

            mTvClassifyName.setTextColor(textColor);
            mTvClassifyName.setText(bean.getClassifyName());
        }

        /**
         * 设置视图的点击事件
         *
         * @param bean 类别数据
         */
        public void setListener(final SearchClassifiesBean bean) {
           itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   if (mOnItemSelectedListener != null && !bean.isSelected()) {
                       mOnItemSelectedListener.onItemSelected(mList, bean, getAdapterPosition());
                   }
               }
           });
        }
    }

    /**
     * 设置菜单条目的点击事件
     */
    public interface OnItemSelectedListener {
        void onItemSelected(List<SearchClassifiesBean> searchClassifiesBeans, SearchClassifiesBean bean, int position);
    }

    private OnItemSelectedListener mOnItemSelectedListener;

    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        mOnItemSelectedListener = onItemSelectedListener;
    }
}
